// 消息数据类型
type MessageType = string | Blob | ArrayBuffer | ArrayBufferView
class Socket {
    socket: WebSocket
    constructor(url: string) {
        this.socket = new WebSocket(url);
        //事件监听
        this.socket.onopen = this.onopen;
        this.socket.onerror = this.onerror;
        this.socket.onclose = this.onclose;
        this.socket.onmessage = this.onmessage;
    }
    // websocket连接成功 状态为opened 时触发
    onopen(this: WebSocket, ev: Event): any {
    }
    // websocket 使用过程中发生错误触发
    onerror(this: WebSocket, ev: Event) {
    }
    // websocket 关闭连接时触发
    onclose(this: WebSocket, ev: CloseEvent) {
    }
    // 当接收到服务端发送来的消息时触发
    onmessage(this: WebSocket, ev: MessageEvent<any>) {
        // 消息具体数据
        //ev.data

    }
    // 发送数据到服务端
    send(data: MessageType) {
        data = data || JSON.stringify({
            "id": client_uuid,
            "nickname": nickname,
            "message": nickname_message
        });
        this.socket.send(data)
    }

    // 关闭websocket 连接，可选的状态码和关闭原因
    close(code?: number, reason?: string) {
        this.socket.close(code, reason)
    }
}